package pl.edu.pw.stud.ltrzaska.policy.xml;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pw.stud.ltrzaska.policy.entity.Filesystem;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemObject;
import pl.edu.pw.stud.ltrzaska.policy.entity.Group;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

public class GroupConverter extends DirectoryConverter implements Converter {

	@Override
	public boolean canConvert(Class clazz) {
		return (clazz == Group.class);
	}
	
	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		Group group = new Group();

		ConverterCommons.preLoopUnmarshal(reader, group);
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			String attrName = reader.getNodeName();

			ConverterCommons.inLoopUnmarshal(reader, context, group);
			if ("children".equals(attrName)) {
				group.setChildren((ArrayList<FilesystemObject>) context.convertAnother(group, List.class));
			}
			reader.moveUp();
		}
		return group;
	}

}
